package com.supinfo.food.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;

import com.supinfo.food.dao.ThemeDao;
import com.supinfo.food.entities.Theme;

/**
 * Theme DAO
 * @author Syn
 */
public class JpaThemeDao implements ThemeDao{
	
	/** Entity manager */
	private EntityManager entityManager;
	
	public JpaThemeDao() {
		entityManager = ManagerDAO.getEntityManager();
	}
	
	@Override
	public Theme addTheme(Theme theme) {
		entityManager.getTransaction().begin();
		entityManager.persist(theme);
		entityManager.getTransaction().commit();
		return theme;
	}

	@Override
	public Theme findTheme(Long id) {
		return entityManager.find(Theme.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Theme> getAllThemes() {
		return entityManager.createQuery("SELECT t FROM Theme").getResultList();
	}

	@Override
	public void removeTheme(Theme theme) {
		entityManager.getTransaction().begin();
		entityManager.remove(theme);		
		entityManager.getTransaction().commit();
	}

	@Override
	public Theme updateTheme(Theme theme) {
		entityManager.getTransaction().begin();
		entityManager.merge(theme);
		entityManager.getTransaction().commit();
		return theme;
	}

}
